package com.mvpandroiddemo.net;

import android.text.TextUtils;

import com.mvpandroiddemo.utils.Constance;
import com.mvpandroiddemo.utils.SharedPreferenceHelper;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


/**
 * @Description $description$
 * @Author owner
 * @CreateTime 2019/10/17 15:20
 **/
public class AddCookiesInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();

        SharedPreferenceHelper preferenceHelper = SharedPreferenceHelper.getInstance();
        String userName = preferenceHelper.getString(Constance.Cache_user_name);
        String pwd = preferenceHelper.getString(Constance.Cache_user_pwd);
        if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(pwd)) {
            builder.addHeader("Cookie", "loginUserName=" + userName);
            builder.addHeader("Cookie", "loginUserPassword=" + pwd);
        }

        return chain.proceed(builder.build());
    }
}
